/**
 * @author 黄波
 * 创建日期：2011Oct 7, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.common.bo.CommonBo;
import com.platform.course.bean.CourseBean;
import com.platform.course.bo.CourseBo;
import com.platform.os.bo.AccessBo;
import com.platform.os.util.Util;
import com.platform.pojo.AccessRecord;
import com.platform.pojo.CourseInfo;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResType;
import com.platform.pojo.SchoolProperty;
import com.platform.pojo.UserInfo;
import com.platform.res.bean.ResInfoBean;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 加载首页信息
 */
public class EntryAction extends NJCCSuport {
	
	private List<ResType> alltypes = new ArrayList<ResType>();	//所有资源类型列表

	private List<ResType> types = new ArrayList<ResType>();	//一级资源类型列表
	private List<ResType> typelist1 = new ArrayList<ResType>();	//资源子类型列表1
	private List<ResType> typelist2 = new ArrayList<ResType>();	//资源子类型列表2
	private List<ResType> typelist3 = new ArrayList<ResType>();	//资源子类型列表3
	private List<ResType> typelist4 = new ArrayList<ResType>();	//资源子类型列表4
	private List<ResType> typelist5 = new ArrayList<ResType>();	//资源子类型列表5
	
	private List<ResInfoBean> list1 = new ArrayList<ResInfoBean>();	//主页显示大类别列表1
	private List<ResInfoBean> list2 = new ArrayList<ResInfoBean>();	//主页显示大类别列表2
	private List<ResInfoBean> list3 = new ArrayList<ResInfoBean>();	//主页显示大类别列表3
	private List<ResInfoBean> list4 = new ArrayList<ResInfoBean>();	//主页显示大类别列表4
	private List<ResInfoBean> list5 = new ArrayList<ResInfoBean>();	//主页显示大类别列表5

	private List<ResInfoBean> recmdRes = new ArrayList<ResInfoBean>();	//推荐资源列表
	private List<ResInfoBean> favRes = new ArrayList<ResInfoBean>();	//下载排行资源信息
	
	private List<CourseBean> courselist = new ArrayList<CourseBean>();	//课程互动列表
	
	private List<UserInfo> drusers = new ArrayList<UserInfo> ();	//共享达人



	@Override
	public String execute() throws Exception {
		ResBo resBo = ResBo.getInstance();
		UserBo userBo = UserBo.getInstance();
		//获得一级资源类型列表
		alltypes = resBo.getTypes();
		types = resBo.getTypes("0");
		//查询二级资源类型列表
		if(types != null) {
			if(types.size() > 0 && types.get(0) != null) {
				typelist1 = resBo.getTypes(types.get(0).getId().toString());
				//查询该大类别资源列表（10条）
				List<ResInfo> infolist = (List<ResInfo>) resBo.getResInfoByType(types.get(0).getId().toString())[1];
				for(ResInfo item : infolist) {
					ResInfoBean bean = new ResInfoBean();
					bean.setInfo(item);
					if(alltypes != null) {
						bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
					}
					UserInfo user = userBo.getUser(item.getUploadUserId().toString());
					if(user != null) {
						bean.setUserName(user.getRealName());
					} else {
						bean.setUserName(null);
					}
//					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
					list1.add(bean);
				}
			}
			if(types.size() > 1 && types.get(1) != null) {
				typelist2 = resBo.getTypes(types.get(1).getId().toString());
				//查询该大类别资源列表（10条）
				List<ResInfo> infolist = (List<ResInfo>) resBo.getResInfoByType(types.get(1).getId().toString())[1];
				for(ResInfo item : infolist) {
					ResInfoBean bean = new ResInfoBean();
					bean.setInfo(item);
					if(alltypes != null) {
						bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
					}
					UserInfo user = userBo.getUser(item.getUploadUserId().toString());
					if(user != null) {
						bean.setUserName(user.getRealName());
					} else {
						bean.setUserName(null);
					}
//					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
					list2.add(bean);
				}
			}
			if(types.size() > 2 && types.get(2) != null) {
				typelist3 = resBo.getTypes(types.get(2).getId().toString());
				//查询该大类别资源列表（10条）
				List<ResInfo> infolist = (List<ResInfo>) resBo.getResInfoByType(types.get(2).getId().toString())[1];
				for(ResInfo item : infolist) {
					ResInfoBean bean = new ResInfoBean();
					bean.setInfo(item);
					if(alltypes != null) {
						bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
					}
					UserInfo user = userBo.getUser(item.getUploadUserId().toString());
					if(user != null) {
						bean.setUserName(user.getRealName());
					} else {
						bean.setUserName(null);
					}
//					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
					list3.add(bean);
				}
			}
			if(types.size() > 3 && types.get(3) != null) {
				typelist4 = resBo.getTypes(types.get(3).getId().toString());
				//查询该大类别资源列表（10条）
				List<ResInfo> infolist = (List<ResInfo>) resBo.getResInfoByType(types.get(3).getId().toString())[1];
				for(ResInfo item : infolist) {
					ResInfoBean bean = new ResInfoBean();
					bean.setInfo(item);
					if(alltypes != null) {
						bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
					}
					UserInfo user = userBo.getUser(item.getUploadUserId().toString());
					if(user != null) {
						bean.setUserName(user.getRealName());
					} else {
						bean.setUserName(null);
					}
//					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
					list4.add(bean);
				}
			}
			if(types.size() > 4 && types.get(4) != null) {
				typelist5 = resBo.getTypes(types.get(4).getId().toString());
				//查询该大类别资源列表（10条）
				List<ResInfo> infolist = (List<ResInfo>) resBo.getResInfoByType(types.get(4).getId().toString())[1];
				for(ResInfo item : infolist) {
					ResInfoBean bean = new ResInfoBean();
					bean.setInfo(item);
					if(alltypes != null) {
						bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
					}
					UserInfo user = userBo.getUser(item.getUploadUserId().toString());
					if(user != null) {
						bean.setUserName(user.getRealName());
					} else {
						bean.setUserName(null);
					}
//					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
					bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
					list5.add(bean);
				}
			}
		}
		
		
		//查询推荐资源列表
		List<ResInfo> recmdResList = resBo.getRecmdResInfo();
		if(recmdResList != null) {
			if(recmdResList.size() > 4) {
				recmdResList = recmdResList.subList(0, 4);//截取四个推荐资源
			}
			for(ResInfo item : recmdResList) {
				ResInfoBean bean = new ResInfoBean();
				bean.setInfo(item);
				if(alltypes != null) {
					bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
				}
				UserInfo user = userBo.getUser(item.getUploadUserId().toString());
				if(user != null) {
					bean.setUserName(user.getRealName());
				} else {
					bean.setUserName(null);
				}
//				bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
				bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
				recmdRes.add(bean);
			}
		}
		
		//查询下载排行资源列表
		List<ResInfo> faResInfoList = (List<ResInfo>) resBo.getFaResInfo()[1];
		for(ResInfo item : faResInfoList) {
			ResInfoBean bean = new ResInfoBean();
			bean.setInfo(item);
			if(alltypes != null) {
				bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
			}
			UserInfo user = userBo.getUser(item.getUploadUserId().toString());
			if(user != null) {
				bean.setUserName(user.getRealName());
			} else {
				bean.setUserName(null);
			}
//			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
			favRes.add(bean);
		}
		
		//查询课程互动信息列表
		CourseBo courseBo = CourseBo.getInstance();
		//公共属性
		CommonBo commonBo = CommonBo.getInstance();
		Object[] objs = courseBo.getCourseInfoForEntry();
		List<CourseInfo> courseInfos = (List)objs[1];
		if(courseInfos != null) {
			for(CourseInfo item : courseInfos) {
				CourseBean bean = new CourseBean();
				//课程实体
				bean.setItem(item);
				//任课老师
				bean.setTeacher(userBo.getUser(item.getUserId().toString()).getRealName());
				//班级名称
				SchoolProperty sp = commonBo.getSchoolProperty(item.getClassId().toString());
				if(sp != null) {
					bean.setClassName(sp.getName());
					bean.setClassNum(sp.getClassNum().toString());
				}
				courselist.add(bean);
			}
		}
		
		//查询10位共享达人信息
		drusers = (List)(userBo.getDRUsers()[1]);
		return SUCCESS;
	}
	

	/**
	 * @param typeId
	 * @return
	 */
	private ResType getTypeName(Integer typeId, List<ResType> types) {
		for(ResType item : types) {
			if(item.getId() == typeId) {
				return item;
			}
		}
		return null;
	}
	
	public List<ResType> getTypes() {
		return types;
	}

	public void setTypes(List<ResType> types) {
		this.types = types;
	}

	public List<CourseBean> getCourselist() {
		return courselist;
	}


	public void setCourselist(List<CourseBean> courselist) {
		this.courselist = courselist;
	}


	public List<ResType> getTypelist1() {
		return typelist1;
	}


	public void setTypelist1(List<ResType> typelist1) {
		this.typelist1 = typelist1;
	}


	public List<ResType> getTypelist2() {
		return typelist2;
	}


	public void setTypelist2(List<ResType> typelist2) {
		this.typelist2 = typelist2;
	}


	public List<ResType> getTypelist3() {
		return typelist3;
	}


	public void setTypelist3(List<ResType> typelist3) {
		this.typelist3 = typelist3;
	}


	public List<ResType> getTypelist4() {
		return typelist4;
	}


	public void setTypelist4(List<ResType> typelist4) {
		this.typelist4 = typelist4;
	}


	public List<ResType> getTypelist5() {
		return typelist5;
	}


	public void setTypelist5(List<ResType> typelist5) {
		this.typelist5 = typelist5;
	}


	public List<ResInfoBean> getRecmdRes() {
		return recmdRes;
	}


	public void setRecmdRes(List<ResInfoBean> recmdRes) {
		this.recmdRes = recmdRes;
	}


	public List<ResInfoBean> getFavRes() {
		return favRes;
	}


	public void setFavRes(List<ResInfoBean> favRes) {
		this.favRes = favRes;
	}


	public List<ResInfoBean> getList1() {
		return list1;
	}


	public void setList1(List<ResInfoBean> list1) {
		this.list1 = list1;
	}


	public List<ResInfoBean> getList2() {
		return list2;
	}


	public void setList2(List<ResInfoBean> list2) {
		this.list2 = list2;
	}


	public List<ResInfoBean> getList3() {
		return list3;
	}


	public void setList3(List<ResInfoBean> list3) {
		this.list3 = list3;
	}


	public List<ResInfoBean> getList4() {
		return list4;
	}


	public void setList4(List<ResInfoBean> list4) {
		this.list4 = list4;
	}


	public List<ResInfoBean> getList5() {
		return list5;
	}


	public void setList5(List<ResInfoBean> list5) {
		this.list5 = list5;
	}


	public List<ResType> getAlltypes() {
		return alltypes;
	}


	public void setAlltypes(List<ResType> alltypes) {
		this.alltypes = alltypes;
	}



	public List<UserInfo> getDrusers() {
		return drusers;
	}


	public void setDrusers(List<UserInfo> drusers) {
		this.drusers = drusers;
	}

	
}
